package xserver;

import org.apache.mina.common.IoSession;

import xgame.AbstractXresult;
import xgame.IXoutput;

/**
 * 游戏输出对象
 * 
 * @author AfritXia 
 * @version $Rev: 17 $
 *
 */
public class XgameOutput implements IXoutput {
	/**
	 * IO 会话对象
	 * 
	 */
	private IoSession _ioSession = null;

	/**
	 * 类参数构造器
	 * 
	 * @param session 
	 * @throws IllegalArgumentException if session == null 
	 * 
	 */
	public XgameOutput(IoSession session) {
		if (session == null) {
			throw new IllegalArgumentException("session is null");
		}

		this._ioSession = session;
	}

	@Override
	public void write(AbstractXresult result) {
		if (result == null) {
			return;
		}

		this._ioSession.write(result);

		if (result.getSubResultList() != null) {
			for (AbstractXresult subResult : result.getSubResultList()) {
				// 获取会话 Id
				long sessionId = subResult.getSessionId();

				// 获取游戏会话
				XgameSession gameSession = XgameSessionManager.getGameSession(sessionId);

				if (gameSession == null) {
					continue;
				}
				
				// 获取输出会话
				IoSession outputSession = gameSession.getIoSession();

				if (outputSession == null) {
					continue;
				}

				// 输出自结果
				outputSession.write(subResult);
			}
		}
	}
}
